import random

from scrapy import Request
from scrapy import Spider
from selenium import webdriver

from config import config
from Ttsg_scrapy.items import TtsgScrapyItem
import re
import time
class TtsgScrapySpider(Spider):
    name = 'Ttsg_scrapy'

    def __init__(self, pipe):
        self.pipe = pipe
        self.driver = webdriver.Chrome()

        self.start_urls = []
        for index in range(0, config.scrapy.maxPageNum):
            i = random.randint(1, config.scrapy.maxPageNum*config.scrapy.pageStep)
            url = f'https://detail.zol.com.cn/cell_phone_index/subcate57_0_list_1_0_1_2_0_{i}.html'
            self.start_urls.append(url)


    def parse(self, response):
        # self.printLog(response.url)
        item = TtsgScrapyItem()
        list_selector = response.xpath('//div[@class="wrapper"]/div[4]//div[1]//div[4]/ul[@class="clearfix"]/li')
        self.printLog(list_selector)
        for one_selector in list_selector:
            # try:
            image_url = one_selector.xpath('a/img[1]/@src').extract()
            if image_url == []:
                image_url = '无图片链接'
            else:
                image_url = response.urljoin(image_url[0])
            self.printLog('图片链接：',image_url)

            price = one_selector.xpath('div[1]/span[2]/b[2]/text()').extract()
            if price == []:
                price = '无'
            else:
                price = price[0]
            self.printLog('价格：',price)

            detail_url = one_selector.xpath('a/@href').extract()
            if detail_url == []:
                detail_url = '无'
            else:
                detail_url = response.urljoin(detail_url[0])
            self.printLog('详情页链接：', detail_url)

            evaluate = one_selector.xpath('div[2]/span[2]/text()').extract()
            if evaluate == []:
                evaluate = '无'
            else:
                evaluate = evaluate[0]
            self.printLog('点评：',evaluate)

            item['image_url'] = image_url
            item['price'] = price
            item['evaluate'] = evaluate

            #跳转到详情页
            yield Request(detail_url,
                          meta={
                              'item':item.copy() #定义一个key为item,然后将解析后的item值放置在这
                          }, callback=self.detailinfo_parse)
            # except:
            #     pass
    def detailinfo_parse(self,response):
        time.sleep(config.scrapy.timeInterval)
        item = response.meta['item'].copy()
        title = response.xpath('//div[@class="product-model page-title clearfix"]/h1[@class="product-model__name"]/text()').extract()
        if title == []:
            title = '无'
        elif len(title) == 1:
            title = title[0].replace('\n','').strip()
        else:
            try:
                title = title[1].replace('\n','').strip()
            except:
                pass
        self.printLog('标题:',title)

        #CPU型号
        goods_cpu = response.xpath('//li[@class="info-item"]/div/a/text()').extract()
        if goods_cpu == []:
            goods_cpu = '无'
        else:
            goods_cpu = goods_cpu[0]
        self.printLog('CPU型号:',goods_cpu)
        #运行内存
        play_memory = response.xpath('//li[@class="info-item t-ramrongliang-liuchang"]/div[1]/a/text()').extract()
        if play_memory == []:
            play_memory = '无'
        else:
            play_memory = play_memory[0]
        self.printLog('运行内存:',play_memory)
        #机身内存
        goods_memory = response.xpath('//div[@class="ver-mo-box__list clearfix"]/a/text()').extract()
        if goods_memory == []:
            goods_memory = '无'
        else:
            goods_memory = goods_memory[0]
        self.printLog('机身内存:',goods_memory)
        #屏幕分辨率
        resolving_power = response.xpath('//li[@class="info-item t-fenbianlv-hd"]/div[1]/span/text()').extract()
        if resolving_power == []:
            resolving_power = '无'
        else:
            resolving_power = resolving_power[0]
        self.printLog('屏幕分辨率:',resolving_power)
        #后摄主像素
        pixel = response.xpath('//li[@class="info-item t-xiangsu-hd"]/div[1]/span/text()').extract()
        if pixel == []:
            pixel = '无'
        else:
            pixel = pixel[0]
        self.printLog('后摄主像素',pixel)

        item['title'] = title
        item['goods_title'] = title
        item['goods_cpu'] = goods_cpu
        item['play_memory'] = play_memory
        item['goods_memory'] = goods_memory
        item['resolving_power'] = resolving_power
        item['pixel'] = pixel

        yield item
        
    def printLog(self, msg, *args):
        if(config.log.logShow):
            print(msg, *args)


